<?php

namespace app\admin\controller;

use app\admin\service\ArticleService;
use app\common\util\ErrorCode;

class ArticleController extends CommonController
{
    // 获取文章类型数据的分页列表
    public function getArticleTypeList(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getArticleTypeList();
        return $this->return_result($this->returnData);
    }

    // 添加文章类型
    public function toAddArticleType(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->toAddArticleType();
        return $this->return_result($this->returnData);
    }

    // 更新文章类型
    public function toUpdateArticleType(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->toUpdateArticleType();
        return $this->return_result($this->returnData);
    }

    // 文章类型，根据$type操作
    public function getArticleTypeToOperate($_type){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        switch ($_type){
            case "updateOrder": //更新权重（文章类型）
                $method = "toUpdateOrderByArticleType";
                break;
            case "delete": // 删除文章类型
                $method = "toDelArticleType";
                break;
            case "getChoose": // 挑选出所有的文章类型
                $method = "getChooseArticleType";
                break;
            case "updateStatus": // 修改文章类型的状态
                $method = "toUpdateStatusArticleType";
                break;
            case "articleListById": // 类型ID获取文章列表
                $method = "getArticleListById";
                break;
            default:
                return $this->returnData = ErrorCode::$errorCode["fail"];
                break;
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->{$method}();
        return $this->return_result($this->returnData);
    }

    // 文章类型的详细内容
    public function getArticleTypeDetail(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getArticleTypeDetail();
        return $this->return_result($this->returnData);
    }

    // 获取文章的分页列表
    public function getArticleList(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getArticleList();
        return $this->return_result($this->returnData);
    }

    // 添加文章
    public function toAddArticle(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->toAddArticle();
        return $this->return_result($this->returnData);
    }

    // 更新文章
    public function toUpdateArticle(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->toUpdateArticle();
        return $this->return_result($this->returnData);
    }

    // 文章的详细内容
    public function getArticleDetail(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getArticleDetail();
        return $this->return_result($this->returnData);
    }

    // 文章，根据$type操作
    public function getTypeToOperate($_type){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        switch ($_type){
            case "delete": // 删除文章
                $method = "toDelArticle";
                break;
            case "updateStatus": // 修改文章的状态
                $method = "toUpdateStatusArticle";
                break;
            default:
                return $this->returnData = ErrorCode::$errorCode["fail"];
                break;
        }
        $service = $this->app->invokeClass(ArticleService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->{$method}();
        return $this->return_result($this->returnData);
    }
}